/*
 * @作者: Fultom
 * @创建时间: 2025-07-10 09:16:33
 * @最近修改时间: 2025-07-10 10:07:30
 * @功能:第三方能力方法
 */

import { StorageKey } from './constant'
import { AppGetStorageSync, AppSetStorageSync, AppToast } from './publicMethod'

/**
 * 支付
 * @param options
 * @param params orderSn: 订单号, totalFee: 总金额 type: 订单类型
 * @param success 成功回调
 * @param error   失败回调
 */
export const AppPay = (options: {
  params: { orderSn: string; totalFee: number; type: number }
  success: () => void
  error?: (error: any) => void
}) => {
  uni.showLoading({ title: '生成订单中...' })
  //TODO 支付
}

/**
 * 字节登录获取openid 和 unionid
 * @param success
 */
export const AppAuthLogin = (
  success: (openid: string, unionid: string, sessionKey: string) => void,
) => {
  const openidT = AppGetStorageSync<string>(StorageKey.openid)
  const unionidT = AppGetStorageSync<string>(StorageKey.unionid)
  const sessionKeyT = AppGetStorageSync<string>(StorageKey.sessionKey)
  if (openidT && unionidT && sessionKeyT) {
    success(openidT, unionidT, sessionKeyT)
  } else {
    //TODO 登录授权
  }
}

export const AppGetOpenId = {}

/**
 * 后续项目新增枚举值参照以下地址
 * @link https://uniapp.dcloud.io/api/other/authorize
 */
export enum PermissionScope {
  userInfo = 'scope.userInfo', //用户信息
  camera = 'scope.camera', //拍照、选择相册
}

const getPermissionContent = (scope: PermissionScope): string => {
  switch (scope) {
    case PermissionScope.userInfo:
      return '检测到您没打开获取用户信息权限，是否去设置打开？'
    case PermissionScope.camera:
      return '检测到您没打开摄像头权限，是否去设置打开？'
    default:
      return '检测到没有此权限，是否去设置打开？'
  }
}

/**权限申请 */
export const PermissionHelp = (scope: PermissionScope, success: () => void) => {
  uni.authorize({
    scope: scope,
    success() {
      success()
    },
    fail(res) {
      uni.showModal({
        content: getPermissionContent(scope),
        confirmText: '确认',
        cancelText: '取消',
        success: (res) => {
          if (res.confirm) {
            uni.openSetting({
              success: (res) => {
                console.log(res)
              },
              fail: (res) => {
                AppToast(res)
              },
            })
          }
        },
      })
    },
  })
}
